local Action_PlayerStartNewRound = 
{
    MaxRoundWaitTime = 0;            -- 回合最长等待时长
}

--√
function Action_PlayerStartNewRound:Execute()
    print("Action_PlayerStartNewRound:Execute")
    local NextTeamIndex = UGCGameSystem.GameState.CurRoundTeamIndex
    -- 迅捷行动则开启另一个回合
    print(string.format("Action_PlayerStartNewRound CurRoundTeamIndex is  [%d]",NextTeamIndex))--0
    local IsPlayAnotherRound = false

    -- 广播客户端显示剩余时间提示
    UnrealNetwork.CallUnrealRPC_Multicast(UGCGameSystem.GameState, "Multicast_ShowRemainTimeTips", true);
    -- 显示选择卡牌提示，不应该广播，在下面通知客户端单播

    -- UnrealNetwork.CallUnrealRPC_Multicast(UGCGameSystem.GameState, "Multicast_ShowSelectCardTips", true);
    
    if UGCGameSystem.GameState.CanPlayerStartAnotherRound then
        -- 打出迅捷行动，再进行一回合
        UGCGameSystem.GameState.CanPlayerStartAnotherRound = false
        IsPlayAnotherRound = true
    else 
        -- 找到下一个，还未结束游戏的玩家
        repeat
            NextTeamIndex = NextTeamIndex % 4 + 1
        until(UGCGameSystem.GameState.PlayerInfos[NextTeamIndex].PlayerPawn~=nil and UGCGameSystem.GameState:HasTeamFinishedGame(NextTeamIndex) == false and NextTeamIndex ~= UGCGameSystem.GameState.CurRoundTeamIndex)
        if NextTeamIndex == UGCGameSystem.GameState.CurRoundTeamIndex then
            print("Action_PlayerStartNewRound:Execute Error: Cannot Find Next Player, Game Should Already Be Finished!")
            return true
        end
    end
    print(string.format("CurRoundTeamIndex number is [%d]",NextTeamIndex))--1,第一个玩家
    UGCGameSystem.GameState.CurRoundTeamIndex = NextTeamIndex
    UGCGameSystem.GameState.CurRoundStatus = ERoundStatus.WaitForSelectCard
    --开始倒计时UI时间的更新
    UGCGameSystem.GameState.CurRoundRemainTime = self.MaxRoundWaitTime
    --UGCGameSystem.GameState.AeroplaneChessRoundNum = UGCGameSystem.GameState.AeroplaneChessRoundNum + 1
    local PlayerInfo = UGCGameSystem.GameState.PlayerInfos[UGCGameSystem.GameState.CurRoundTeamIndex]

    --print(string.format("CurRoundTeamIndexsdffgdt[%d]",PlayerInfo.PlayerKey))
    -- ？？？？？？？？？？
    UnrealNetwork.CallUnrealRPC_Multicast(UGCGameSystem.GameState, "Multicast_NewRoundStart", NextTeamIndex, PlayerInfo.PlayerKey, IsPlayAnotherRound);

    -- 通知客户端 显示选择卡牌提示和面板按钮？？使用哪个
    --local PlayerKey = self.PlayerInfos[self.CurRoundTeamIndex].PlayerKey
    --local PlayerController = UGCGameSystem.GetPlayerControllerByPlayerKey(PlayerKey)
    --local CardInfos = UGCGameSystem.GameState.PlayerInfos[UGCGameSystem.GameState.CurRoundTeamIndex].CardInfos
    --UnrealNetwork.CallUnrealRPC(PlayerController, PlayerController, "ClientRPC_ShowSelectCardTips", true);
    --UnrealNetwork.CallUnrealRPC(PlayerController, PlayerController, "ClientRPC_StartCardSelection", CardInfos)
    --if not UGCGameSystem.GameState:IsValidOnlinePlayer(PlayerInfo.PlayerKey) or PlayerInfo.IsInAutoPlay == true then
        -- 托管
        --UGCGameSystem.GameState:DoRollDice()
    --end
    self.bEnableActionTick = true
    -- 回合开始的时间
    self.RoundStartTime = GameplayStatics.GetRealTimeSeconds(self)
    print("Action_PlayerStartNewRound:Execute end ")
	return true
end

--√
function Action_PlayerStartNewRound:Update(DeltaTime)
    print("Action_PlayerStartNewRound:Update")
    -- 计算剩余时间
    local CurrentRealTime = GameplayStatics.GetRealTimeSeconds(self)
    if UGCGameSystem.GameState.IsStopDiceTimer then
        return;
    end
    local RemainTime = self.MaxRoundWaitTime - (CurrentRealTime - self.RoundStartTime)
    if RemainTime < 0 then
        RemainTime = 0
    end
    UGCGameSystem.GameState.CurRoundRemainTime = RemainTime
    
    if self.RemainTimeInSeconds ~= math.ceil(RemainTime) then
        self.RemainTimeInSeconds = math.ceil(RemainTime)
        ugcprint(string.format("Action_PlayerStartNewRound:RemainTimeInSeconds[%d]", self.RemainTimeInSeconds))
    end

    if RemainTime <= 0 then
        print("Action_PlayerStartNewRound RemainTime <= 0")
        self:PlayerRoundTimeUp()
	end

end

-- 回合时间结束
function Action_PlayerStartNewRound:PlayerRoundTimeUp()
    ugcprint(string.format("Action_PlayerStartNewRound:PlayerRoundTimeUp"))
    self.bEnableActionTick = false
    -- 时间结束未操作，则进行托管，操作后，CurRoundStatus改变则不会进行下面逻辑
    -- if UGCGameSystem.GameState.CurRoundStatus == ERoundStatus.WaitForRollDice then
    --     print(string.format("Action_PlayerStartNewRound:PlayerRoundTimeUp Start AutoPlay"))
    --     UGCGameSystem.GameState:GetCurRoundPlayerInfo().IsInAutoPlay = true
    --     --UnrealNetwork.CallUnrealRPC(PlayerController, PlayerController, "ClientRPC_TrusteeshipTips")
    -- end
    -- 自动完成回合
    -- UGCGameSystem.GameState:AutoCompleteRound()
    
    -- 没有自动完成回合，直接进入下一个人的回合
    LuaQuickFireEvent("PlayerRoundEnd", self)
end

return Action_PlayerStartNewRound